package com.snack.common.code;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA1算法 2010-8-4上午11:41:05 SHA1
 * 
 * @author lamfire
 */
public class SHA1 {
	
	public static String sign(String message) {
		return hash(message);
	}
	
	public static String hash(String message) {
		byte[] encode;
		try {
			byte[] data = message.getBytes("UTF-8");
			encode = digest(data);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return HEX.encodeHexString(encode);
	}

	public static byte[] digest(byte[] datas) {
		return getMessageDigest().digest(datas);
	}

	public static String digestHex(byte[] datas) {
		return HEX.encodeHexString(digest(datas));
	}

	public static MessageDigest getMessageDigest() {
		try {
			return MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}

	}
}
